package com.pzhu.acargid.certificate;

import com.alibaba.fastjson.JSON;
import com.pzhu.acargid.coauthor.CoauthorUtil;
import com.pzhu.acargid.doc.DocService;
import com.pzhu.acargid.domain.Certificate;
import com.pzhu.acargid.domain.Coauthor;
import com.pzhu.acargid.domain.Doc;
import com.pzhu.acargid.until.Result;
import com.pzhu.acargid.vo.CertificateVO;
import com.pzhu.file.FileController;
import com.pzhu.file.PdfImage;
import com.pzhu.file.utils.FileUtil;
import com.pzhu.security.token.JwtTokenService;
import org.apache.commons.io.FileUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/certificate")
public class CertificateController {
    @Autowired
    private DocService docService;
    @Autowired
    private CertificateService certificateService;
    @Autowired
    private CoauthorUtil coauthorUtil;
    private final Logger logger = LoggerFactory.getLogger(CertificateController.class);

    @PostMapping
    public Result upload(HttpServletRequest request, @RequestParam("data") String data, @RequestParam String images) throws IOException {
        Integer userid = JwtTokenService.getClaim(request, "userid").asInt();
        CertificateVO CertificateVO = JSON.parseObject(data, CertificateVO.class);
        List<PdfImage> list = JSON.parseArray(images, PdfImage.class);
        List<String> collect = list.stream().map(PdfImage::getImage).collect(Collectors.toList());
        List<Coauthor> coauthors = CertificateVO.getCoauthors();
        CertificateVO.setUserId(userid);
        CertificateVO.setRemoved(0);
        CertificateVO.setModType("certificate");
        CertificateVO.setModDbTable("certificate");
        Doc doc = new Doc();
        Certificate certifate = new Certificate();
        BeanUtils.copyProperties(CertificateVO, doc);
        BeanUtils.copyProperties(CertificateVO, certifate);
        doc.setId(null);
        certifate.setDocId(null);
        docService.save(doc);
        certifate.setDocId(doc.getId());
        certificateService.save(certifate);
        String savePath = FileUtil.getSavePath(userid, "paper") + doc.getId();
        FileUtil.makeDir(savePath, logger);
        FileController.savePDF(CertificateVO.getTitle(), savePath + File.separator + CertificateVO.getTitle() + ".pdf", collect);
        return coauthorUtil.getResult(coauthors, doc, userid);
    }

    @PutMapping
    public Result update(HttpServletRequest request, @RequestParam("data") String data, @Param("images") String images) throws IOException {
        Integer userid = JwtTokenService.getClaim(request, "userid").asInt();
        CertificateVO CertificateVO = JSON.parseObject(data, CertificateVO.class);
        List<PdfImage> list = JSON.parseArray(images, PdfImage.class);
        List<String> collect = list.stream().map(PdfImage::getImage).collect(Collectors.toList());
        String savePath = FileUtil.getSavePath(userid, "certificate") + CertificateVO.getId();
        FileUtil.makeDir(savePath, logger);
        FileUtils.cleanDirectory(new File(savePath));
        FileController.savePDF(CertificateVO.getTitle(), savePath + File.separator + CertificateVO.getTitle() + ".pdf", collect);
        List<Coauthor> coauthors = CertificateVO.getCoauthors();
        CertificateVO.setUserId(userid);
        CertificateVO.setRemoved(0);
        CertificateVO.setModType("certificate");
        CertificateVO.setModDbTable("certificate");
        CertificateVO.setUserId(userid);
        Doc doc = new Doc();
        Certificate certificate = new Certificate();
        BeanUtils.copyProperties(CertificateVO, doc);
        BeanUtils.copyProperties(CertificateVO, certificate);
        docService.updateById(doc);
        certificateService.updateById(certificate);
        return coauthorUtil.update(coauthors, doc, userid);
    }
}
